package com.store.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.store.app.R;
import com.store.app.activity.CashierActivity;
import com.store.app.bean.GoodsBean;
import com.store.app.bean.GoodsListBean;

import java.util.List;

/**
 * Created by Administrator on 2016/3/2.
 */
public class GridViewAdapter extends BaseAdapter {
    private List<GoodsBean> goodsBeanList;
    private Context context;
    public GridViewAdapter(Context context ,List<GoodsBean> goodsBeanList){
        this.context  = context;
        this.goodsBeanList = goodsBeanList;
    }
    @Override
    public int getCount() {
        return goodsBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.img_text_tag, null);
        LinearLayout llAll = (LinearLayout) convertView.findViewById(R.id.ll_all);
        ImageView iv = (ImageView) convertView.findViewById(R.id.ItemImage);//商品图片
        TextView tvName = (TextView) convertView.findViewById(R.id.trade_name);//商品名称
        TextView tvPrice = (TextView) convertView.findViewById(R.id.commodity_prices);//商品价格
        tvName.setText(goodsBeanList.get(position).getGoods_name());
        tvPrice.setText(goodsBeanList.get(position).getMarket_price() + "");
        DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.default_picture_s)
        .showImageForEmptyUri(R.drawable.default_picture_s)
        .showImageOnFail(R.drawable.default_picture_s) .build();
        ImageLoader.getInstance().displayImage(goodsBeanList.get(position).getPath_url(),iv,options);
        llAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsListBean bean = new GoodsListBean();
                bean.setCount(1);
                bean.setGoodsName(goodsBeanList.get(position).getGoods_name());
                bean.setPrice(goodsBeanList.get(position).getMarket_price() + "");
                CashierActivity.goodsListBeanList.add(bean);
                CashierActivity.handler.sendEmptyMessage(1);
            }
        });
        return convertView;
    }
}
